<?php
/** Patient_list
	* 
	* @package Pharmacy Intervention 
	* @subpackage Controller
	* @category Setup 
	* @author Ray Nowell 
	*/  
class Patient_list extends MY_Controller {

	function Patient_list()
	{
		parent::MY_Controller();
	}
	
	/*
	 * PRIVATE FUNCTIONS (e.g. the ones that begin with an underscore
	 */	

	function _get_extra_doc_ready_script()
	{
		$retVal	=	'$("#_uploadPatientLink").click(function() 
									{
										popup_upload_patient();
										return true;
									});';
		return $retVal;
	}
	
	function _get_upload_patient_script()
	{
 		$retVal = 'function popup_upload_patient()
													{
														var popupLeft = parseInt((screen.availWidth/2) - (490/2));
																						var popupTop = parseInt((screen.availHeight/2) - (230/2));
																						window.open(\''.site_url().'/upload\',\'popup\',\'width=\' + 490 + \',height=\' + 230 + \',scrollbars=no,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no,left=\' + popupLeft + \',top=\' + popupTop + \'\');
																						return false;
													};';	
		return $retVal;
	}
		

	/*
	 * PUBLIC FUNCTIONS (e.g. can be accessed from the browser
	 */
	
	function index()
	{		
		define("POPUP_HEIGHT", 300);
		define("POPUP_WIDTH", 800);
		
		$data['title'] = COMPANY_NAME.' -- Patient List';
		$data['heading'] = 'Patients';
	
		$data['additional_javascript_doc_ready_functions'] = get_grid_doc_ready_javascript().$this->_get_extra_doc_ready_script();
		$data['additional_jquery_javascript'] = get_grid_functions('patient', POPUP_HEIGHT, POPUP_WIDTH).$this->_get_upload_patient_script();
		
		//removing link..will add java script to do the links
		$data['add_link'] = '<input id="_gridAddTop" type="image" border="0" language="javascript" onclick="return false;" alt="Insert" title="Insert" src="'.base_url().'images/add.png"  style="cursor: pointer;"/>';
		$data['view_link'] = '<input class="GridButton" id="_gridViewTop" type="image" disabled="disabled" border="0" language="javascript" onclick="return false;" alt="View" title="View" src="'.base_url().'images/view_disabled.png"  style="cursor: pointer;"/>';
		$data['copy_link'] = '<input class="GridButton" id="_gridCopyTop" type="image" disabled="disabled" border="0" language="javascript" onclick="return false;" alt="Copy" title="Copy" src="'.base_url().'images/copy_disabled.png"  style="cursor: pointer;"/>';
		$data['delete_link'] = '<input class="GridButton" id="_gridDeleteTop" type="image" disabled="disabled" border="0" language="javascript" onclick="return false;" alt="Delete" title="Delete" src="'.base_url().'images/delete_disabled.png"  style="cursor: pointer;"/>';

		$this->load->library('table');
		$this->table->set_heading('<input type="checkbox" id="_chkMaster">','Pat. Number','Last Name','First Name');
		
		$this->load->model('Patient_model');
		
		//$this->output->enable_profiler(TRUE);	
		$patient_array = $this->Patient_model->get_patient_list();
		
		//loop through the results and checkbox the id
		foreach($patient_array as $row)
		{
			$java_script_text	=	'popup_edit_single('.$row['patient_id'].');return false;'; //Must do this before replacing text on line below
			$row['patient_id']= '<input class="GridCheckbox" type="checkbox" name="_chkIntervention'.$row['patient_id'].'" >';
			$edit_link_atts = array('title' => 'Edit',
									'onclick' => $java_script_text 
									);
			$row['patient_id_number'] = anchor('#',$row['patient_id_number'],$edit_link_atts);
			$this->table->add_row($row);
		}
		
		$local_table_template = set_table_template();
		$this->table->set_template($local_table_template); 
		$local_table = $this->table->generate();
		$data['patient_list'] = str_replace('<td><input class="GridCheckbox"','<td class="GridCheckboxTd"><input class="GridCheckbox"',$local_table);
		
		//Check flash data to see if there is any text to display to the user 
		$deleted_info = $this->session->flashdata('deleted_info');
		if (!empty($deleted_info))
			$data['deleted_info']	=	$deleted_info;
		
		$data['view'] = 'patient_list_view';
		$this->load->vars($data);
		$this->load->view('master_view', $data);	
			
	}
	
	function delete()
	{
		$this->load->model('Patient_model');
		$ret_val	=	$this->Patient_model->Delete_patient();
		if (count($ret_val) > 0)
			$this->session->set_flashdata('deleted_info', $ret_val);
		
		redirect('patient_list');
	}

	function refresh()
	{
		redirect('patient_list');
	}
	
	/*
	 * The lookup function used to return a list of patients found based on the criteria posted.
	 */
	function lookup()
		{

			//$this->output->enable_profiler(TRUE);
			 $this->load->model('Patient_model');
				// process posted form data (the requested product)
				$keyword = $this->input->post('term');
				$data['response'] = 'false'; //Set default response
				$query = $this->Patient_model->seek_patients($keyword); //Search DB
				if( ! empty($query) )
				{
						//print_r[$row];
						$data['response'] = 'true'; //Set response
						$data['message'] = array(); //Create array
						foreach( $query as $row )
										{
											$data['message'][] = array(	'id'		=>	$row->patient_id,
																		'id_number'	=>	$row->patient_id_number,
																		'value'		=>	$row->full_patient_description
															); //Add a row to array
						}				}
				if(IS_AJAX)
				{
						echo json_encode($data); //echo json string if ajax request
				}
				else
				{
						$this->load->view('patient_list/index',$data); //Load html view of search results
				}
		
		} // end of lookup	
	
}

/* End of file patient_list.php */
/* Location: ./system/application/controllers/patient_list.php */